/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package user;
import connectivity.QueryManager;
import helpers.Encrypt;
import model.User;


/**
 *
 * @author Wouter
 */
public class UserHandler
{
	private static boolean loggedIn = false;
	private static User userData = null;
	private static QueryManager queryManager;
	public static Encrypt hash = new Encrypt();
	
	public static boolean isLoggedIn()
	{
		return UserHandler.loggedIn;
	}

	public static void setQueryManager(QueryManager querymanager)
	{
		UserHandler.queryManager = querymanager;
	}
	
	public static boolean loginAttempt(String username, char[] password)
	{
		// Encrypt password
		String password2 = UserHandler.hash.encrypt(password);
		
		System.out.println("Login attempt: " + username + ", password: " + password + ", encrypted: " + password2);
		User loginAttempt = UserHandler.queryManager.checkUserLogin(username, password2);
		
		if(loginAttempt != null)
		{
			UserHandler.userData = loginAttempt;
			System.out.println("Logged in with id #" + loginAttempt.id);
			return true;
		}
		else
		{
			System.out.println("Login failed :(");
			return false;
		}
	}
	
	public static boolean logUserIn(String username)
	{
		User user = UserHandler.queryManager.getUser("gebruikersnaam", "Harrie");
		
		if(user != null)
		{
			UserHandler.loggedIn = true;
			UserHandler.userData = user;
			System.out.println("Forcefully logged in as " + username);
			return true;
		}
		
		return false;
	}
}